package com.Mobile.Action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.Entity.Userdata;
import com.Entity.Users;
import com.Service.UserdataService;
import com.Service.UsersService;

public class MobileUpdateProfileAction implements ServletResponseAware{
     //更新个人资料(孕妇)
	private HttpServletResponse response;//response闁诲海鏁搁、濠囨寘?
	private int flag=0;
	@Autowired
	private UsersService usersservice;
	@Autowired
	private UserdataService userdataservice;
	private Users user=new Users();
	private Userdata userdata=new Userdata();
	
	public void UpdateProfile() throws ServletException,IOException {
		
		String userid=ServletActionContext.getRequest().getParameter("UserID");
		String nickname=ServletActionContext.getRequest().getParameter("Nickname");
		String mobile=ServletActionContext.getRequest().getParameter("Mobile");
		String idcard=ServletActionContext.getRequest().getParameter("IDcard");//身份证
		String cardid=ServletActionContext.getRequest().getParameter("CardID");//卡号
		String birthday=ServletActionContext.getRequest().getParameter("Birthday");
		//当传送过来的nickname是中文时会乱码
		nickname = new String(nickname.getBytes("ISO-8859-1"), "UTF-8");
		
		
		userid = java.net.URLDecoder.decode(userid,"UTF-8");
		nickname = java.net.URLDecoder.decode(nickname,"UTF-8");
		mobile = java.net.URLDecoder.decode(mobile,"UTF-8");
		idcard = java.net.URLDecoder.decode(idcard,"UTF-8");
		int cardid1 = Integer.parseInt(java.net.URLDecoder.decode(cardid,"UTF-8"));
		birthday = java.net.URLDecoder.decode(birthday,"UTF-8");
		
		JSONObject returnjson=new JSONObject();
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json;charset=utf-8"); 
		int years=Integer.parseInt(birthday.substring(0,4));
		int months=Integer.parseInt(birthday.substring(5,7));
		int days=Integer.parseInt(birthday.substring(8,10));
		System.out.println(years);
		System.out.println(months);
		System.out.println(days);
		try {
			PrintWriter out = response.getWriter();
			int id=Integer.parseInt(userid);
			
			user=this.usersservice.getById(id);
			if (user!=null){
				flag=1;
			    userdata=this.userdataservice.getById(id);	
			    user.setNickName(nickname);
			    userdata.setPhoneNumber(mobile);
			    userdata.setCardId(cardid1);
			    userdata.setIdcard(idcard);
			    userdata.setYears(years);
			    userdata.setMonths(months);
			    userdata.setDays(days);
			    this.usersservice.save(user);
			    this.userdataservice.save(userdata);
			    
				returnjson.put("flag", flag);
			}else{
				flag=0;
				returnjson.put("flag", flag);
			}						
			out.println(returnjson.toString());
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		this.response=response;
	}

	public Users getUser() {
		return user;
	}

	public void setUser(Users user) {
		this.user = user;
	}

	public Userdata getUserdata() {
		return userdata;
	}

	public void setUserdata(Userdata userdata) {
		this.userdata = userdata;
	}

}
